﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq.Expressions;
using System.Text;
using Microsoft.VisualStudio.DebuggerVisualizers;
using Newtonsoft.Json;

namespace ImageVisualizer
{
    class ImageVisualizer : DialogDebuggerVisualizer
    {
        public ImageVisualizer() : base(FormatterPolicy.Json)
        {

        }
        public ImageVisualizer(FormatterPolicy preferredFormatterPolicy) : base(preferredFormatterPolicy)
        {
        }

        protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
        {
            IVisualizerObjectProvider3 objectProvider3 = objectProvider as IVisualizerObjectProvider3;
            //IVisualizerObjectProvider2 objectProvider2 = objectProvider as IVisualizerObjectProvider2;


            //var v2 = objectProvider.GetData();
            //var v3 = objectProvider.TransferData(null);


            //var v21 = objectProvider2.GetData();
            //var v22 = objectProvider2.TransferData(null);

            //var v31 = objectProvider3.GetObject<Image>();
            //var v32 = objectProvider3.TransferObject<Image>(v2);
            //var v33 = objectProvider3.DeserializeFromJson<Image>(v22);

            //var deserializableObj = objectProvider3.GetDeserializableObject();

            //Image image = null;
            //if(!deserializableObj.IsBinaryFormat)
            //{
            //    var base64 = deserializableObj.ToString();
            //    byte[] bytes = Convert.FromBase64String(base64);
            //    using(var ms = new MemoryStream(bytes))
            //    {
            //        image = Image.FromStream(ms);
            //    }
            //}
            //else
            //{
            //    image = deserializableObj.ToObject<Image>();
            //}

            var image = objectProvider3.GetObject<Image>();
            if(image != null)
            {
                using(ImageForm form = new ImageForm())
                {
                    form.ShowInTaskbar = false;
                    form.Set(image);

                    windowService.ShowDialog(form);
                }
            }

        }
    }
    // 自定义序列化处理器（解决Bitmap序列化问题）
    public class ImageVisualizerObjectSource : VisualizerObjectSource
    {
        public override void TransferData(object target, Stream incomingData, Stream outgoingData)
        {
            Serialize(outgoingData, target);
            //if(target is Image image)
            //{
            //    using(var ms = new MemoryStream())
            //    {
            //        image.Save(ms, ImageFormat.Png);

            //        //var bytes = ms.ToArray();
            //        byte[] bytes = Encoding.UTF8.GetBytes(Convert.ToBase64String(ms.ToArray()));

            //        outgoingData.Write(bytes, 0, bytes.Length);
            //    }
            //}
            //// 2. 支持反向数据流（编辑后传回）
            //if(incomingData != null && incomingData.Length > 0)
            //{
            //    using(var ms = new MemoryStream())
            //    {
            //        incomingData.CopyTo(ms);
            //        var modifiedImage = Image.FromStream(ms);
            //        ReplaceTarget(target, modifiedImage);
            //    }
            //}
        }

        public override void GetData(object target, Stream outgoingData)
        {
            Serialize(outgoingData, target);
            //if(target is Image image)
            //{
            //    using(var ms = new MemoryStream())
            //    {
            //        image.Save(ms, ImageFormat.Png);

            //        //var bytes = ms.ToArray();
            //        byte[] bytes = Encoding.UTF8.GetBytes(Convert.ToBase64String(ms.ToArray()));

            //        outgoingData.Write(bytes, 0, bytes.Length);
            //    }
            //}
        }
    }
}